package Day13.IO.zIFuLiu;

import java.io.*;

public class Test3 {
    /*

    拷贝文件夹考虑子文件夹

     */

    public static void main(String[] args) throws IOException {
        File fi = new File("D:\\aaa");
        File fo = new File("C:\\");

        if (fi.equals(fo)){
            System.out.println("目标文件夹是源文件夹的子文件夹");
        }else {
            copyDir(fi,fo);
        }
    }

    public static void copyDir(File src, File dest) throws IOException {
        File newDemo1 = new File(dest,src.getName());
        newDemo1.mkdirs();

        //从数据源中获取数据
        File[] files = src.listFiles();
        //遍历数组获取每一个文件或文件夹对象
        for (File file : files) {
            //当前对象是否是文件
            if (file.isFile()){
                FileInputStream fi = new FileInputStream(file);
                FileOutputStream fo = new FileOutputStream(new File(newDemo1,file.getName()));

                int i;
                byte[] bys = new byte[1024];
                while ((i = fi.read(bys))!=-1){
                    fo.write(bys,0,i);
                }
                fi.close();
                fo.close();



            }else {
                //如果是文件，递归调用方法
                copyDir(file,newDemo1);
            }
        }

    }
}
